// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import ElementUI from 'element-ui';
import qs from 'qs';
//解决浏览器对ES6中的promise无法支持
import BabelPolyfill from 'babel-polyfill';
// import $ from "jquery";
import echarts from 'echarts';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App'
import router from './router'
import Lib from '@/assets/js/lib.js'
import VueAxios from 'vue-axios'
//静态资源 公用样式common
import "./assets/css/common.css";
import "./assets/css/base.css";
import dateFormat from 'dateformat';
//公用select下拉组件数据
import {getSelectData} from './assets/js/getSelectData';
// 获取数据字典信息
import {getDicData} from './assets/js/sysDictionary';
import {downloadFile} from './assets/js/fileDownLoad';
import {showMsg} from './components/common/message/message';
// 获取当前采购组和当前采购组织
import SysPurOrg from './assets/js/sysPurOrg';
import FileUtil from './assets/js/fileUtil';
//打印
import {print} from './assets/js/print';

// 富文本插件
// import '../static/icons/iconfont.css';
import VueContextMenu from 'vue-contextmenu'
import 'vue-contextmenu/style/css/font-awesome.min.css'
Vue.use(VueContextMenu)

// import httpTimeout from './components/api/httpTimeout'
// Vue.use(httpTimeout)
Vue.use(Vuex)
Vue.use(ElementUI)
Vue.use(qs)
//解决浏览器对ES6中的promise无法支持
Vue.use(BabelPolyfill)

Vue.prototype.getSelectData = getSelectData;
Vue.prototype.getDicData = getDicData;
Vue.prototype.dateFormat = dateFormat;
Vue.prototype.$echarts = echarts
Vue.prototype.getPurOrgSelect = SysPurOrg.getSelect;
Vue.prototype.getPurGroupSelect = SysPurOrg.getGroupSelect;
Vue.prototype.deleteFile = FileUtil.deleteFile;
Vue.config.productionTip = false;
Vue.prototype.downloadFile = downloadFile;
Vue.prototype.showMsg = showMsg;
Vue.prototype.print = print;

const store = new Vuex.Store({
  state: {
    httping:false,
    securityId: '',
    modules : [],
    leftModules : []
  },
  mutations: {
    updateSecurityId (state,securityId) {
      state.securityId = securityId;
      window.localStorage.securityId = securityId;
    },
    updateModules(state,modules){
      state.modules = modules
    },
    updateLeftModules(state,leftModules){
      state.leftModules = leftModules
    },
    httpStaring(state){
      state.httping=true;
    },
    httpEnding(state){
      state.httping=false;
    }
  }
});

router.onReady(()=>{
  if (window.location.pathname === '/login' || window.location.pathname === '/'
    || window.location.pathname === '/HomePage'
    || window.location.pathname === '/Details'
    || window.location.pathname.indexOf('/ActiveAccount/') > -1
    || window.location.pathname.indexOf('/inviteRegister/') > -1
    ){ //访问为登录路径时，直接返回
    window.localStorage.removeItem('securityId');
    return;
  }
  if(window.localStorage.securityId){ //从localstorage中取出securityId
    store.commit('updateSecurityId',window.localStorage.securityId);
  }else{
    router.push({path: '/login',  query: {redirect: encodeURIComponent(router.currentRoute.fullPath)}});
  }
});

// 获取当前登陆用户的ID
Vue.prototype.getCurrUserId = function() {
  return store.state.securityId;
};

//请求前处理
Lib.ajax.interceptors.request.use(function(config){
  let url = config.url.split('?');
  if (/\/login$/.test(url[0]) || /\/portal\/query$/.test(url[0])) {
    store.commit('httpStaring');
    return config;
  }
  let index = config.url.indexOf('?');
  if (index > -1 && config.url.substr(index + 1) !== '') {
    config.url = config.url + '&security_id=' + store.state.securityId;
  } else {
    config.url = config.url + '?security_id=' + store.state.securityId;
  }
  store.commit('httpStaring');
  return config;
},function(err){
  store.commit('httpEnding');
  return Promise.reject(err);
});

//请求后处理
Lib.ajax.interceptors.response.use(function(response){
  if(response.data.success==false){
    Vue.prototype.$message(response.data.message);
  }
  store.commit('httpEnding');
  return response;
},function(err){
  Vue.prototype.$message(err);
  store.commit('httpEnding');
  // securityId错误返回登陆页面
  if(err.response && err.response.status === 401){
    store.commit('updateSecurityId','');
    window.localStorage.removeItem('securityId');
    router.replace({
      path: '/',
      query: {redirect: encodeURIComponent(router.currentRoute.fullPath)}
    });
  }
  return Promise.reject(err);
});

//form表单重置
Vue.prototype.resetForm = function(formName) {
  this.$refs[formName].resetFields();
};
Vue.use(VueAxios, Lib.ajax);
Vue.prototype.lib = Lib;
//lib.js文件挂载到Vue.prototype

// Vue.prototype.getSecurityUrl = function(url,param){
//   return url+'?security_id='+store.state.securityId;
// }
//
// Vue.prototype.securityGet = function(url, params) {
//   return new Promise((resolve, reject) => {
//     if(store.state.securityId==''){
//       this.axios.get('/index').then(res=>{
//         var result = res.data;
//         store.commit('updateSecurityId',result);
//         this.axios.get(this.getSecurityUrl(url), params).then(function (response) {
//           resolve(response.data)
//         })
//         .catch(function (err) {
//           reject(err)
//         })
//       })
//     }else{
//       this.axios.get(this.getSecurityUrl(url), params).then(function (response) {
//         resolve(response.data)
//       })
//       .catch(function (err) {
//         reject(err)
//       })
//     }
//
//
//   })
// }
//
// Vue.prototype.securityPost = function(url, params) {
//   return new Promise((resolve, reject) => {
//     if(store.state.securityId==''){
//       this.axios.get(this.lib.config.host.system+'/index').then(res=>{
//         var result = res.data;
//         store.commit('updateSecurityId',result);
//         this.axios.post(this.getSecurityUrl(url), params).then(function (response) {
//           resolve(response.data)
//         })
//         .catch(function (err) {
//           reject(err)
//         })
//       })
//     }else{
//       this.axios.post(this.getSecurityUrl(url), params).then(function (response) {
//         resolve(response.data)
//       })
//       .catch(function (err) {
//         reject(err)
//       })
//     }
//   })
// }

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
